<template>
  <div class="to-do-header">
    <h1>{{ title }}</h1>
    <div>
      <input type="text" v-model="thing" v-on:keyup.enter="addThing" />
      <button v-on:click="addThing">添加待办</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      required: true,
      type: String,
    },
  },
  data() {
    return {
      thing: "",
    };
  },
  methods: {
    addThing() {
      if (this.thing.trim === "") {
        alert("空值");
      }
      const thingobj = {
        name: this.thing,
        id: +new Date(),
      };
      // 通知父组件修改数据
      this.$emit("addThings", thingobj);
      this.thing = "";
    },
  },
};
</script>

<style scoped>
.to-do-header {
  width: 500px;
  height: 100px;
}
</style>